//package com.camunda.workflow.camunda;
//
//import org.camunda.bpm.engine.BadUserRequestException;
//import org.camunda.bpm.engine.IdentityService;
//import org.camunda.bpm.engine.ProcessEngineException;
//import org.camunda.bpm.engine.identity.*;
//import org.camunda.bpm.engine.impl.ServiceImpl;
//import org.camunda.bpm.engine.impl.cmd.*;
//import org.camunda.bpm.engine.impl.identity.*;
//import org.camunda.bpm.engine.impl.persistence.entity.IdentityInfoEntity;
//import org.camunda.bpm.engine.impl.util.EnsureUtil;
//import org.camunda.bpm.engine.impl.util.ExceptionUtil;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
//public class CustomIdentityService extends ServiceImpl implements IdentityService {
//    private ThreadLocal<Authentication> currentAuthentication = new ThreadLocal<>();
//    @Override
//    public boolean isReadOnly() {
//        return commandExecutor.execute(new IsIdentityServiceReadOnlyCmd());
//    }
//
//    @Override
//    public User newUser(String userId) {
//        return commandExecutor.execute(new CreateUserCmd(userId));
//    }
//
//    @Override
//    public void saveUser(User user) {
//        saveUser(user, false);
//    }
//    public void saveUser(User user, boolean skipPasswordPolicy) {
//        try {
//            commandExecutor.execute(new SaveUserCmd(user, skipPasswordPolicy));
//        } catch (ProcessEngineException ex) {
//            if (ExceptionUtil.checkConstraintViolationException(ex)) {
//                throw new BadUserRequestException("The user already exists", ex);
//            }
//            throw ex;
//        }
//    }
//    @Override
//    public UserQuery createUserQuery() {
//        return commandExecutor.execute(new CreateUserQueryCmd());
//    }
//
//    @Override
//    public void deleteUser(String userId) {
//        commandExecutor.execute(new DeleteUserCmd(userId));
//    }
//
//    @Override
//    public void unlockUser(String userId) {
//        commandExecutor.execute(new UnlockUserCmd(userId));
//    }
//
//    @Override
//    public Group newGroup(String groupId) {
//        return commandExecutor.execute(new CreateGroupCmd(groupId));
//    }
//
//    @Override
//    public NativeUserQuery createNativeUserQuery() {
//        return commandExecutor.execute(new CreateNativeUserQueryCmd());
//    }
//
//    @Override
//    public GroupQuery createGroupQuery() {
//        return commandExecutor.execute(new CreateGroupQueryCmd());
//    }
//
//    @Override
//    public void saveGroup(Group group) {
//        try {
//            commandExecutor.execute(new SaveGroupCmd(group));
//        } catch (ProcessEngineException ex) {
//            if (ExceptionUtil.checkConstraintViolationException(ex)) {
//                throw new BadUserRequestException("The group already exists", ex);
//            }
//            throw ex;
//        }
//    }
//
//    @Override
//    public void deleteGroup(String groupId) {
//        commandExecutor.execute(new DeleteGroupCmd(groupId));
//    }
//
//    @Override
//    public void createMembership(String userId, String groupId) {
//
//    }
//
//    @Override
//    public void deleteMembership(String userId, String groupId) {
//        commandExecutor.execute(new DeleteMembershipCmd(userId, groupId));
//    }
//
//    @Override
//    public Tenant newTenant(String tenantId) {
//        return commandExecutor.execute(new CreateTenantCmd(tenantId));
//    }
//
//    @Override
//    public TenantQuery createTenantQuery() {
//        return commandExecutor.execute(new CreateTenantQueryCmd());
//    }
//
//    @Override
//    public void saveTenant(Tenant tenant) {
//        try {
//            commandExecutor.execute(new SaveTenantCmd(tenant));
//        } catch (ProcessEngineException ex) {
//            if (ExceptionUtil.checkConstraintViolationException(ex)) {
//                throw new BadUserRequestException("The tenant already exists", ex);
//            }
//            throw ex;
//        }
//    }
//
//    @Override
//    public void deleteTenant(String tenantId) {
//        commandExecutor.execute(new DeleteTenantCmd(tenantId));
//    }
//
//    @Override
//    public void createTenantUserMembership(String tenantId, String userId) {
//        commandExecutor.execute(new CreateTenantUserMembershipCmd(tenantId, userId));
//    }
//
//    @Override
//    public void createTenantGroupMembership(String tenantId, String groupId) {
//        commandExecutor.execute(new CreateTenantGroupMembershipCmd(tenantId, groupId));
//    }
//
//    @Override
//    public void deleteTenantUserMembership(String tenantId, String userId) {
//        commandExecutor.execute(new DeleteTenantUserMembershipCmd(tenantId, userId));
//    }
//
//    @Override
//    public void deleteTenantGroupMembership(String tenantId, String groupId) {
//        commandExecutor.execute(new DeleteTenantGroupMembershipCmd(tenantId, groupId));
//    }
//
//    @Override
//    public boolean checkPassword(String userId, String password) {
//        return commandExecutor.execute(new CheckPassword(userId, password));
//    }
//
//    @Override
//    public PasswordPolicyResult checkPasswordAgainstPolicy(String password) {
//        return checkPasswordAgainstPolicy(getPasswordPolicy(), password, null);
//    }
//
//    @Override
//    public PasswordPolicyResult checkPasswordAgainstPolicy(String candidatePassword, User user) {
//        return checkPasswordAgainstPolicy(getPasswordPolicy(), candidatePassword, user);
//    }
//
//    @Override
//    public PasswordPolicyResult checkPasswordAgainstPolicy(PasswordPolicy policy, String password) {
//        return checkPasswordAgainstPolicy(policy, password, null);
//    }
//
//    @Override
//    public PasswordPolicyResult checkPasswordAgainstPolicy(PasswordPolicy policy, String candidatePassword, User user) {
//        EnsureUtil.ensureNotNull("policy", policy);
//        EnsureUtil.ensureNotNull("password", candidatePassword);
//
//        List<PasswordPolicyRule> violatedRules = new ArrayList<>();
//        List<PasswordPolicyRule> fulfilledRules = new ArrayList<>();
//
//        for (PasswordPolicyRule rule : policy.getRules()) {
//            if (rule.execute(candidatePassword, user)) {
//                fulfilledRules.add(rule);
//            } else {
//                violatedRules.add(rule);
//            }
//        }
//        return new PasswordPolicyResultImpl(violatedRules, fulfilledRules);
//    }
//
//    @Override
//    public PasswordPolicy getPasswordPolicy() {
//        return commandExecutor.execute(new GetPasswordPolicyCmd());
//    }
//
//    @Override
//    public void setAuthenticatedUserId(String authenticatedUserId) {
//        setAuthentication(new Authentication(authenticatedUserId, null));
//    }
//
//    @Override
//    public void setAuthentication(String userId, List<String> groups) {
//        setAuthentication(new Authentication(userId, groups));
//    }
//
//    @Override
//    public void setAuthentication(String userId, List<String> groups, List<String> tenantIds) {
//        setAuthentication(new Authentication(userId, groups, tenantIds));
//    }
//
//    @Override
//    public void setAuthentication(Authentication auth) {
//        if(auth == null) {
//            clearAuthentication();
//        } else {
//            if (auth.getUserId() != null) {
//                EnsureUtil.ensureValidIndividualResourceId("Invalid user id provided", auth.getUserId());
//            }
//            if (auth.getGroupIds() != null) {
//                EnsureUtil.ensureValidIndividualResourceIds("At least one invalid group id provided", auth.getGroupIds());
//            }
//            if (auth.getTenantIds() != null) {
//                EnsureUtil.ensureValidIndividualResourceIds("At least one invalid tenant id provided", auth.getTenantIds());
//            }
//
//            currentAuthentication.set(auth);
//        }
//    }
//
//    @Override
//    public Authentication getCurrentAuthentication() {
//        return currentAuthentication.get();
//    }
//
//    @Override
//    public void clearAuthentication() {
//        currentAuthentication.remove();
//    }
//
//    @Override
//    public void setUserPicture(String userId, Picture picture) {
//        commandExecutor.execute(new SetUserPictureCmd(userId, picture));
//    }
//
//    @Override
//    public Picture getUserPicture(String userId) {
//        return commandExecutor.execute(new GetUserPictureCmd(userId));
//    }
//
//    @Override
//    public void deleteUserPicture(String userId) {
//        commandExecutor.execute(new DeleteUserPictureCmd(userId));
//    }
//
//    @Override
//    public void setUserInfo(String userId, String key, String value) {
//        commandExecutor.execute(new SetUserInfoCmd(userId, key, value));
//    }
//
//    @Override
//    public String getUserInfo(String userId, String key) {
//        return commandExecutor.execute(new GetUserInfoCmd(userId, key));
//    }
//
//    @Override
//    public List<String> getUserInfoKeys(String userId) {
//        return commandExecutor.execute(new GetUserInfoKeysCmd(userId, IdentityInfoEntity.TYPE_USERINFO));
//    }
//
//    @Override
//    public void deleteUserInfo(String userId, String key) {
//        commandExecutor.execute(new DeleteUserInfoCmd(userId, key));
//    }
//
//    @Override
//    public void setUserAccount(String userId, String userPassword, String accountName, String accountUsername, String accountPassword, Map<String, String> accountDetails) {
//        commandExecutor.execute(new SetUserInfoCmd(userId, userPassword, accountName, accountUsername, accountPassword, accountDetails));
//    }
//
//    @Override
//    public List<String> getUserAccountNames(String userId) {
//        return commandExecutor.execute(new GetUserInfoKeysCmd(userId, IdentityInfoEntity.TYPE_USERACCOUNT));
//    }
//
//    @Override
//    public Account getUserAccount(String userId, String userPassword, String accountName) {
//        return commandExecutor.execute(new GetUserAccountCmd(userId, userPassword, accountName));
//    }
//
//    @Override
//    public void deleteUserAccount(String userId, String accountName) {
//        commandExecutor.execute(new DeleteUserInfoCmd(userId, accountName));
//    }
//}
